#include "fighter.h"
#include "fig_craft.h"
#include "fig_bullet.h"
#include "fig_sfx_manager.h"
#include "fig_sfx.h"
#include "fig_sounds.h"

#include "math/facing.h"
#include "dd/dd_man.h"
#include "math/intersection.h"


FIGCraft::FIGCraft() :  FIGLifeObj(),
						m_Rotation(0.0f),
						m_Speed(1.0f),
						m_MaxSpeed(1.0f),
						m_Radius(0.001f),
						m_Color(ColorfRGBA(1.0f,1.0f,1.0f,1.0f)),
						m_pSprite(NULL)						
	
{
	m_Pos = math::Vec2(0.5f,0.5f);
	m_MaxLife = m_Life = 2;
}



void	FIGCraft::Render(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
		return;
    
	ui::UISprite* pSprite = NULL;
    
	if(m_pSprite)
		pSprite = m_pSprite;
	else
		pSprite = &m_Sprite;
    
	if(pSprite)
	{
		if(m_GetHitCounter > 0)
			pSprite->m_Color = ColorfRGBA(1.0f,0.1f,0.1f,1.0f);	
		else
			pSprite->m_Color = m_Color;
        
		pSprite->m_Pos = m_Pos + Offset;
		pSprite->m_Size = pSprite->m_SizeOriginal * Scale;
		pSprite->m_Rotation = m_Rotation + GetExtraRotation();
		pSprite->RenderRotated();
	}
}

void		FIGCraft::RenderShadow(const math::Vec2& Offset, float Scale)
{
	if(!m_IsAlive)
		return;
    
	ui::UISprite* pSprite = NULL;
    
	if(m_pSprite)
		pSprite = m_pSprite;
	else
		pSprite = &m_Sprite;
    
	if(pSprite)
	{
		pSprite->m_Pos = m_Pos + Offset +  math::Vec2(-m_Radius,m_Radius);
		pSprite->m_Color = ColorfRGBA(0.05f,0.05f,0.05f,1.0f);	
		pSprite->m_Size = pSprite->m_SizeOriginal * Scale * 0.6f;
		pSprite->m_Rotation = m_Rotation;
		pSprite->RenderRotated();
	}	
    
}

void	FIGCraft::RenderDebug(const math::Vec2& Offset,float Scale)
{
	if(!m_IsAlive)
		return;

	if(m_Radius <= 0)
		m_Radius = 0.025f;
    
	dd::Manager&	DD = dd::Manager::Instance();
	DD.AddCircle2D( m_Pos + Offset, m_Radius * Scale * GraphicsUtil::W2H , ColorRGBA(255,0,0) );
}

void	FIGCraft::Tick(float dt)
{
	FIGLifeObj::Tick(dt);
    
	if(!m_IsAlive)
		return;
    
    ui::UISprite* pSprite = NULL;
    
	if(m_pSprite)
	{

	}
	else
	{
		m_Sprite.Tick(dt);
	}
	
    
}


void	FIGCraft::Clear()
{
	FIGLifeObj::Clear();
}

void	FIGCraft::Load(const std::string& Name)
{
	m_Sprite.Create(Name);	
	m_Radius = m_Sprite.m_SizeOriginal.x * 0.4f;
    
	
	
}


void	FIGCraft::GoForward(float dt)
{
	math::Vec2 Dir = math::Facing::VectorFromAngle(m_Rotation-180);
	Dir.Normalize();
	
	m_Pos += ( Dir *  dt * m_Speed);
	
}


bool	FIGCraft::IsInSideScreen()
{
	if( m_Pos.x + (m_Sprite.m_SizeOriginal.x*0.5f) < -0.2f)
		return false;
    
	if(m_Pos.x - (m_Sprite.m_SizeOriginal.x*0.5f) > 1.2f)
		return false;
    
	if( m_Pos.y + (m_Sprite.m_SizeOriginal.y*0.5f) < 0)
		return false;
    
	if(m_Pos.y - (m_Sprite.m_SizeOriginal.y*0.5f) > 1)
		return false;
    
	return true;
}

ui::UISprite*		FIGCraft::GetSprite()
{ 
	if(m_pSprite)
		return m_pSprite;
	
	return &m_Sprite; 
} 

